package com.jry.iaspacctivemq.active;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.Queue;
import java.util.HashMap;
import java.util.Map;


/**
 * @author sss
 */
@RestController
@Slf4j
public class ProducerController {

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Resource
    private Queue queue;

    @PostMapping("/queue/test")
    public Map<String, Object> sendQueue(@RequestBody String str) {
        Map<String, Object> stringObjectMap = sendMessage(this.queue, str);
        return stringObjectMap;
    }

    private Map<String, Object> sendMessage(Destination destination, final String message){
        Map<String, Object> res = new HashMap<>();
        String msg;
        int code;
        try {
            jmsMessagingTemplate.convertAndSend(destination, message);
            msg = "success";
            code = 200;
        } catch (Exception e) {
            msg = e.getMessage();
            code = 500;
            log.error("sendMessage error: {}", e.getMessage());
        }
        res.put("msg", msg);
        res.put("code", code);
        return res;
    }

}
